<template>
	<view>
		<view class="top-back">
			<view class="top-name">商品管理（{{count}}）</view>

			<view class="top-right top-right-left" @click="addAction">
				<view class="top-exit">新增</view>
			</view>
			<view class="top-right" :class="screenSelect?'top-right-select':''" @click="selectAction">
				<view :class="screenSelect?'top-contnet top-content-select': 'top-contnet'">筛选</view>
				<image v-if="!screenSelect" class="top-icon" src="../../../static/select.png"></image>
				<image v-if="screenSelect" class="top-icon" src="../../../static/select_select.png"></image>
			</view>
		</view>
		<view class="uni-container">
			<uni-table ref="table" :loading="loading" border stripe emptyText=" ">
				<uni-tr>
					<uni-th align="center">商品id</uni-th>
					<uni-th align="center">商品名称</uni-th>
					<uni-th align="center">商品价格</uni-th>
					<uni-th align="center">图片</uni-th>
					<uni-th align="center" width="80px">当前状态</uni-th>
					<uni-th align="center">操作人</uni-th>
					<uni-th align="center" width="180px">最后操作时间</uni-th>
					<uni-th align="center">操作</uni-th>
				</uni-tr>
				<uni-tr v-for="(item, index) in tableData" :key="index">
					<uni-td align="center"><text selectable="true">{{item.id}}</text></uni-td>
					<uni-td align="left" @tap.stop.prevent>
						<view @click="rowclick(item)"><text selectable="true">{{item.name }}</text></view>
					</uni-td>
					<uni-td align="left"><text selectable="true">{{ item.price}}</text></uni-td>
					<uni-td align="center">
						<image class="header-img" mode="aspectFill" :src="item.picUrls"></image>
					</uni-td>
					<uni-td align="center" @tap.stop.prevent>
						<switch color="#2DA9C5" :checked="item.status.code == '1'" @change.stop="switchChange(item)"
							style="transform:scale(0.7)" />
					</uni-td>
					<uni-td align="center"><text selectable="true">{{ item.editAt?item.editAt:item.createAt }}</text></uni-td>
					<uni-td align="center"><text selectable="true">{{ item.editTime?item.editTime:item.createTime }}</text></uni-td>
					<uni-td align="center">
						<view style="display: flex;justify-content: center;" @tap.stop.prevent>
							<view class="item-blue-text" @click="delAction(item)">删除</view>
							<view style="margin-left: 12px;" class="item-blue-text" @click="clickEditAction(item)">编辑
							</view>
						</view>
					</uni-td>
				</uni-tr>
			</uni-table>

			<view class="search-back" v-if="tableData.length == 0">
				<image class="search-image" :src="pc_search_nodata"></image>
				<view class="search-noresult">没有查询到结果</view>
			</view>
		</view>
		<productDetail v-if="showDetail" :itemID='itemID' @detailClose='clickDetailClose'></productDetail>
		<productAdd v-if="showAdd" @addBnnerClose='clickAddBnnerClose' :itemID='itemID' :editType='editType'
			v-on:successBack='updataRequest()' :rangeList='rangeList'></productAdd>
		<popSelect v-model="showScreen" :x="screenx" :y="screeny" placement="top-end" dynamic>
			<view v-if="showScreen" class="screen-back">
				<uni-forms ref="baseForm" :modelValue="popData" style="width:200px;">
					<uni-forms-item>
						<uni-easyinput v-model="popData.id" placeholder="商品id" />
					</uni-forms-item>
					<uni-forms-item>
						<uni-easyinput v-model="popData.name" placeholder="商品名称" />
					</uni-forms-item>
					<uni-forms-item>
						<selectxy  :zindex=399 ref="selectxy" :value="multipleArr" placeholder="咨询领域" :options="rangeList"
							@change="multipleChange">
						</selectxy>
						
					</uni-forms-item>
					<uni-forms-item>
						<uniselect v-model="popData.status" placeholder='当前状态' :localdata="typeRange"
							@change="typeChange" :clear="false">
						</uniselect>
					</uni-forms-item>
					<view class="select-bottom">
						<view class="select-bottom-left" @click="selectReset">重置</view>
						<view class="select-bottom-right" @click="selectSureAction">筛选</view>
					</view>
				</uni-forms>
			</view>
		</popSelect>
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog type="danger" cancelText="取消" confirmText="确定" title="提示" content="确定要删除吗？"
				@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
		</uni-popup>

	</view>
</template>

<script>
	import productAdd from "@/packageB/PC/productMange/productAdd.vue"
	import popSelect from "@/components/popSelect/popSelect.vue"
	import uniselect from "@/packageB/PC/components/uni-data-select/uni-data-select.vue"
	import selectxy from "@/components/select-cy/select-cy.vue"
	import productDetail from "@/packageB/PC/productMange/productDetail.vue"
	export default {
		components: {
			productAdd,
			popSelect,
			uniselect,
			selectxy,
			productDetail
		},
		data() {
			return {
				showAdd: false,
				tableData: [],

				screenx: 0,
				screeny: 0,
				showScreen: false,
				popData: {
					id: '',
					name: '',
					domainDict: '',
					status: '',
				},
				screenSelect: false,
				typeRange: [{
						value: '3',
						text: "全部"
					},
					{
						value: '0',
						text: "禁用"
					},
					{
						value: '1',
						text: "正常"
					}
				],
				selectItem: {},
				editType: 'add',
				itemID: '',
				rangeList: [],
				multipleArr: [],
				selectArr: [],
				showDetail: false,
				count: 0,
				pc_search_nodata:getApp().globalData.baseImageUrl+'/pc_search_nodata.png'
			}

		},

		mounted() {
			this.requestData();
			this.requestDicData();
		},
		methods: {
			//咨询领域
			async requestDicData() {
				let _this = this
				const res = await this.$myRequest({
					url: "/comm/dict/list/param_consult_domain",
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					var arr = [];
					for (var i = 0; i < res.data.data.length; i++) {
						var item = res.data.data[i]
						item.label = item.name;
						arr.push(item)
					}
					_this.rangeList = arr;
					console.log(_this.rangeList)
				}
			},
			async updataRequest() {
				this.requestData()
			},
			//网络请求示例
			async requestData() {

				let _this = this
				var url = "/admin/goods/list";
				let parmas = {
					size: '1000'
				};

				if (this.screenSelect) {
					url = '/admin/goods/selector'
					if (_this.popData.id) {
						this.$set(parmas, 'id', 'eq:' + _this.popData.id)
					}
					if (_this.popData.name) {
						this.$set(parmas, 'name', 'lk:' + _this.popData.name)
					}
					if (_this.selectArr.length) {

						var dicStr = '';
						for (var i = 0; i < _this.selectArr.length; i++) {

							if (i == 0) {
								dicStr = _this.selectArr[i].value
							} else {
								dicStr += "," + _this.selectArr[i].value
							}
						}
						this.$set(parmas, 'domainDict', 'in:' + dicStr)
					}
					if (_this.popData.status == '0' || _this.popData.status == '1') {
						this.$set(parmas, 'status', _this.popData.status)
					}
				}

				const res = await this.$myRequest({
					url: url,
					method: 'POST', //默认是GET
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.count = res.data.data.count;
					var arr = [];
					for (var i = 0; i < res.data.data.list.length; i++) {
						var item = res.data.data.list[i];
						item.picUrls = _this.$imageUrls({
							picUrls: item.picUrls
						})
						item.price = (item.price / 100).toFixed(2);
						arr.push(item)
						console.log(item.picUrls)
					}
					_this.tableData = arr

				}
			},
			//改变状态
			async switchChange(item) {
				console.log(item)
				var url = "/admin/goods/disable/"; //禁用
				if (item.status.code == '0') {
					url = "/admin/goods/enable/" //启用
				}

				let _this = this
				const res = await this.$myRequest({
					url: url + item.id,
					method: 'PUT',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {

				}
			},

			async requestImage(img) {
				let _this = this;
				const res = await this.$myRequest({
					url: "/comm/oss/url/img?ossPath=" + img + '&token=' + uni.getStorageSync("accessToken"),
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {

				}
			},
			//咨询领域
			multipleChange(item, value) {
				console.log('咨询领域' + item, value);
				this.selectArr = item;
				this.multipleArr = value;
				
			},
			delAction(item) {
				this.selectItem = item;
				this.$refs.alertDialog.open()

			},

			async dialogConfirm() {
				console.log('点击确认')
				let _this = this

				let parmas = {
					'ids': this.selectItem.id,
				}
				const res = await this.$myRequest({
					url: "/admin/goods",
					method: 'DELETE',
					data: parmas,
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.requestData()
				}
			},

			dialogClose() {
				console.log('点击关闭')
			},

			//点击筛选
			selectAction(e) {
				this.screenx = e.touches[0].clientX
				uni.getSystemInfo({
					success: res => {
						this.screenx = res.windowWidth - 30
					}
				})
				this.screeny = e.touches[0].clientY + 4
				this.showScreen = !this.showScreen
			},

			//筛选重置
			selectReset() {
				this.popData = {
					id: '',
					name: '',
					domainDict: '',
					status: '',
				};
				this.$refs.selectxy.handleAllRemove();
				this.screenSelect = false;
				this.requestData();

			},
			//类型选择
			typeChange(e) {
				console.log(e)
			},
			//筛选确认	
			selectSureAction() {
				this.screenSelect = true;
				this.showScreen = !this.showScreen
				this.requestData()
			},
			//点击列
			rowclick(item) {
				console.log('item', item);
				this.itemID = item.id;
				this.showDetail = true;
			},

			//新增
			addAction() {
				this.editType = 'add';
				this.showAdd = true
			},
			//编辑
			clickEditAction(item) {
				this.selectItem = item;
				console.log(this.selectItem);
				this.itemID = item.id;
				this.editType = 'edit';
				this.showAdd = true;
			},

			clickDetailClose() {
				this.showDetail = false;
			},

			clickAddBnnerClose() {
				this.showAdd = false;
			},

		}
	}
</script>

<style scoped>
	.top-back {
		display: flex;
		align-items: center;
		padding: 10px;
	}

	.top-name {
		color: #333;
		font-size: 18px;
		font-weight: bold;
		margin-left: 30px;
	}

	.top-num {
		color: #999;
		font-size: 18px;
		margin-left: 4px;
	}

	.top-contnet {
		font-size: 14px;
		color: #999;
	}

	.top-content-select {
		color: #2DA9C5;
	}

	.top-icon {
		width: 16px;
		height: 16px;
	}

	.top-right {
		cursor: pointer;
		display: flex;
		align-items: center;
		margin-right: 16px;
		margin-left: 16px;
		padding: 5px 10px;
		border-radius: 5px;
	}

	.top-right-select {
		background-color: #E8F7FB;
	}

	.top-right-left {
		margin-left: auto;
	}

	.uni-container {
		margin-left: 40px;
		margin-right: 16px;
		width: auto;
	}

	.search-back {
		width: 100%;
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}

	.search-noresult {
		font-size: 14px;
		color: #333;
		font-weight: 400;
	}

	.search-image {
		width: 150px;
		height: 150px;
	}

	.screen-back {
		padding: 10px;
		width: 300px;
	}

	.header-img {
		width: 30px;
		height: 30px;
	}

	.item-blue-text {
		cursor: pointer;
		font-size: 14px;
		font-weight: 400;
		color: #2DA9C5;
	}

	.top-exit {
		width: 56px;
		height: 30px;
		background: #E8F7FB;
		border-radius: 2px;
		font-size: 14px;
		font-weight: normal;
		color: #2DA9C5;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.select-bottom {
		display: flex;
	}

	.select-bottom-left {
		cursor: pointer;
		height: 32px;
		width: 120px;
		color: #333;
		background: #FFFFFF;
		border-radius: 2px;
		border: 1px solid #EEEEEE;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 14px;
	}

	.select-bottom-right {
		cursor: pointer;
		height: 32px;
		width: 120px;
		color: #fff;
		background: #8ED8E9;
		border-radius: 2px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-size: 14px;
		margin-right: 0px;
		margin-left: auto;
	}
</style>
